package com.burgeon.backend.EnterpriseWeChat.rest.VipClothespress;

import com.burgeon.backend.EnterpriseWeChat.entity.VipFamily;
import com.burgeon.backend.EnterpriseWeChat.entity.Wardrobe;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class getVipWardrobe extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getVipWardrobe(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Wardrobe> getData(String vipCardNo,String token) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(vipCardNo), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Wardrobe> wardrobes = new ArrayList<>();
        for (int i = 0; i <jsonArray.length() ; i++) {
            Wardrobe wardrobe= new Wardrobe();
            wardrobe.setStoreName(jsonArray.getJSONArray(i).getString(0));
            wardrobe.setProductValue(jsonArray.getJSONArray(i).getString(1));
            wardrobe.setSize(jsonArray.getJSONArray(i).getString(2));
            wardrobe.setColor(jsonArray.getJSONArray(i).getString(3));
            wardrobe.setProductName(jsonArray.getJSONArray(i).getString(4));
            wardrobe.setPriceActual(jsonArray.getJSONArray(i).getString(5));
            wardrobe.setDisCount(jsonArray.getJSONArray(i).getString(6));
            wardrobe.setBillDate(jsonArray.getJSONArray(i).getString(7));
            wardrobe.setProductId(jsonArray.getJSONArray(i).getString(8));
            wardrobe.setDocNo(jsonArray.getJSONArray(i).getString(9));
            wardrobe.setType(token);
            wardrobes.add(wardrobe);
        }

        return wardrobes;
    }

    private MultiValueMap Data(String vipCardNo) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_STORE_ID;NAME'");//店名
        colums.add("'M_PRODUCT_ID;VALUE'");//商品名
        colums.add("VALUE2");//尺寸
        colums.add("VALUE1");//颜色
        colums.add("'M_PRODUCT_ID;NAME'");//货号
        colums.add("PRICEACTUAL");//标准价
        colums.add("DISCOUNT");//折扣
        colums.add("BILLDATE");//日期
//        colums.add("'M_PRODUCT_ID;NAME'");//图片
        colums.add("'M_PRODUCT_ID'");//图片
        colums.add("'DOCNO'");//单号

        params.put("columns", colums);

        params.put("table", "'APP_VIP_WARDROBE'");//表名
        params.put("count","true");

        HashMap orderby = new HashMap();
        orderby.put("column","C_STORE_ID");
        orderby.put("asc","true");
        ArrayList list = new ArrayList();
        list.add(orderby);
        params.put("orderby",list);

        HashMap paramss = new HashMap();
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", "'="+vipCardNo+"'");
        params.put("params",paramss);
        System.err.println();

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getVipWardrobe g = new getVipWardrobe("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {//3125
            System.err.println(g.getData("18631556190",""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
